<template>
<div class="main">
  <h3>基本信息</h3>
  <div class="mainmess">
    <div class="singer-img">
      <img :src="this.pic" style="width:100%"/>
    </div>
    <el-upload  :before-upload="beforeAvatorUpload"
                :on-success="handleAvatorSuccess"
                :action="update()"
                class="upload">
      <el-button size="mini">更新图片</el-button>
    </el-upload>
    <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">

      <el-form-item label="用户名">
         <span style="font-size: 17px;margin-left: 30px;">{{user.username}}</span>
      </el-form-item>
      <el-form-item label="国家或地区" prop="area">
        <el-select v-model="ruleForm.area" placeholder="请选择国家或地区">
          <el-option label="中国" value="china"></el-option>
          <el-option label="还是我中国" value="beijing"></el-option>
        </el-select>
      </el-form-item>

      <el-form-item label="性别" prop="sex">
        <el-radio-group v-model="ruleForm.sex" text-color="red">
          <el-radio label="男"></el-radio>
          <el-radio label="女"></el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="个人简介" prop="introduction">
        <el-input type="textarea" v-model="ruleForm.introduction"></el-input>
      </el-form-item>
      <el-form-item label="邮箱">
        <span style="font-size: 17px;margin-left: 30px;">{{this.user.email}}</span>
      </el-form-item>
      <el-form-item label="创建时间">
        <span style="font-size: 17px;margin-left: 30px;">{{this.user.created}}</span>
      </el-form-item>
      <el-form-item label="最近登录时间">
      <span style="font-size: 17px;margin-left: 30px;padding-top: 10px">{{this.user.lastlogintime}}</span>
    </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="submitForm('ruleForm')">立即修改</el-button>
        <el-button @click="resetForm">取消修改</el-button>
      </el-form-item>
    </el-form>
  </div>
</div>
</template>

<script>
import {mixin} from "../../mixins";
import {updateMessage,getPic} from "../../api";
export default {
  name: "UserMessage",
  mixins: [mixin],
  data() {
    return {
      ruleForm: {
        id: '',
        area: '',
        sex: '',
        introduction: ''
      },
      pic:'',
      user: {},
      rules: {
        area: [
          { required: true, message: '国家或地区', trigger: 'change' }
        ],
        sex: [
          { required: true, message: '请选择性别', trigger: 'change' }
        ],
        introduction: [
          { required: true, message: '请填写活动形式', trigger: 'blur' }
        ]
      }
    };
  },
  methods: {
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          if(this.ruleForm.sex==='男'){
            this.ruleForm.sex = 1;
          }else if(this.ruleForm.sex==='女'){
            this.ruleForm.sex = 0;
          }
          this.ruleForm.id = this.$store.getters.getUser.id
           updateMessage(this.ruleForm).then((res)=>{
            const user =  this.$store.getters.getUser
            user.sex = this.ruleForm.sex
            user.area = this.ruleForm.area
            user.introduction = this.ruleForm.introduction
            this.$store.commit("SET_USERINFO",user)
            this.$message.success("修改成功")
            this.$router.push("/info")
          })
        } else {
          return false;
        }
      });
    },
    getpic(){
      getPic(this.$store.getters.getUser.id).then((res)=>{
        this.pic = this.$store.getters.getHost+res.data.pic
      })
    },
    resetForm() {
      this.$message.success("取消成功")
     this.$router.push("/info")
    },
    update(){
      const id = this.user.id
      const host = this.$store.getters.getHost
      this.getpic()
      return `${host}/updateUserPic?id=${id}`
    }
  },
  created() {
     this.getpic();
     this.user = this.$store.getters.getUser;
    if(this.user.sex===1){
       this.ruleForm.sex = '男'
     }else if(this.user.sex===0){
       this.ruleForm.sex = '女'
     }
     this.ruleForm.area = this.user.area;
     this.ruleForm.introduction = this.user.introduction;
  }
}
</script>

<style scoped>
.main{
  background-color: #a1b7ab;
}
h3{
  margin: 0;
  padding: 0;
}
.mainmess{
  /*height: auto;*/
  width: 500px;
  margin: 0 auto;
  padding-right: 80px;
  height: 800px;
  line-height: 100%;
}
.singer-img{
  height: 120px;
  width: 120px;
  background-color: #545c64;
  border-radius: 10px;
  margin: 0 auto;
}
.upload{
  margin-top: 20px;
  margin-left: 212px;
}
.el-button {
  margin-right: 70px;
}
span{
  color: #d65515;
}
</style>
